<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="height: 60px">
              <el-button
                icon="el-icon-sunrise-1"
                size="medium"
                type="primary"
                round
                @click="dialogVisible = true"
              >新增角色</el-button>
            </el-row>
            <!-- 表格 -->
            <el-table
              border=""
              :data="roleList"
            >
              <el-table-column label="序号" width="120" align="center">
                <template slot-scope="scope">
                  {{ scope.$index+1 }}
                </template>
              </el-table-column>
              <el-table-column label="角色名称" width="240" prop="name" align="center" />
              <el-table-column label="描述" prop="description" align="center" />
              <el-table-column label="操作" align="center">
                <!-- <template slot-scope="{row}">  -->
                <template v-slot="{row}">
                  <el-button size="small" type="success" @click="showPermission(row.id)">分配权限</el-button>
                  <el-button
                    size="small"
                    type="primary"
                    icon="el-icon-edit"
                    @click="editRole(row.id)"
                  >编辑</el-button>

                  <el-button
                    size="small"
                    type="danger"
                    icon="el-icon-delete"
                    @click="deleteRole(row.id)"
                  >删除</el-button>
                </template>
              </el-table-column>
            </el-table>
            <!-- 表格 -->
            <!-- 分页组件 -->
            <el-row
              type="flex"
              justify="center"
              align="middle"
              style="height: 60px"
            >
              <el-pagination
                :total="total"
                :page-size="roleConfig.pagesize"
                layout="prev,pager,next"
                background
                @current-change="currentChange"
              />
            </el-row>
            <!-- 分页组件 -->
          </el-tab-pane>
          <el-tab-pane label="公司信息">
            <el-alert
              title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改"
              type="info"
              show-icon
            />
            <el-form label-width="120px" style="margin-top: 50px">
              <el-form-item label="公司名称">
                <el-input v-model="companyInfo.name" disabled style="width: 400px" />
              </el-form-item>
              <el-form-item label="公司地址">
                <el-input v-model="companyInfo.companyAddress" disabled style="width: 400px" />
              </el-form-item>
              <el-form-item label="邮箱">
                <el-input v-model="companyInfo.mailbox" disabled style="width: 400px" />
              </el-form-item>
              <el-form-item label="备注">
                <el-input
                  v-model="companyInfo.remarks"
                  type="textarea"
                  :rows="3"
                  disabled
                  style="width: 400px"
                />
              </el-form-item>
            </el-form>
          </el-tab-pane>
        </el-tabs>
      </el-card>
      <!-- 点击添加编辑弹框 -->
      <el-dialog
        :title="roleForm.id?'编辑角色':'新增角色'"
        :visible="dialogVisible"
        @close="btnCancel"
      >
        <el-form ref="roleForm" :model="roleForm" :rules="rules" label-width="120px">
          <el-form-item label="角色名称" prop="name">
            <el-input v-model="roleForm.name" @keyup.enter.native="onConfirm" />
          </el-form-item>
          <el-form-item label="角色描述" prop="description">
            <el-input v-model="roleForm.description" @keyup.enter.native="onConfirm" />
          </el-form-item>
        </el-form>
        <!-- 底部 -->
        <el-row slot="footer" type="flex" justify="center">
          <el-col :span="6">
            <el-button
              size="mini"
              @click="btnCancel"
            >取 消</el-button>
            <el-button
              type="primary"
              size="mini"
              @click="onConfirm"
            >确 定</el-button>
          </el-col>
        </el-row>
      </el-dialog>
      <!-- 点击添加编辑弹框 -->

      <!-- 分配权限 -->
      <!-- 设置角色权限弹窗 -->
      <el-dialog title="分配权限" :visible="isShowPermission" @close="btnPermCancel">
        <!-- data 是数据数组, 套着一堆对象 props 指定要显示的字段,
        default-expand-all 默认展开所有 -->
        <el-checkbox-group v-model="checkList">
          <!-- 本来多个多选框需要自己 v-for 遍历, 但是这里由树形处理了 -->

          <!-- 每个属性节点都是一个多选框 -->
          <!-- <el-checkbox v-for="item in permList" :key="item.id" :label="item.id">
            {{ item.name }}
          </el-checkbox> -->
          <el-tree :data="permList" default-expand-all>
            <template v-slot="{data}">
              <el-checkbox :label="data.id">
                {{ data.name }}
              </el-checkbox>
            </template>
          </el-tree>
        </el-checkbox-group>
        <template #footer>
          <el-button size="small" type="primary" @click="btnPermOK">确定</el-button>
          <el-button size="small" @click="btnPermCancel">取消</el-button>
        </template>
      </el-dialog>
      <!-- 分配权限 -->
    </div>
  </div>
</template>

<script>
import { addRoleAPI, assignPerm, companyInfoAPI, delRoleAPI, getRoleDetailAPI, getRoleListAPI, updateRoleAPI } from '@/api/setting'
import { mapState } from 'vuex'
import { getPermissionList } from '@/api/permission'
import { listToTree } from '@/utils'
export default {
  data() {
    return {
      companyInfo: {},
      roleList: [],
      total: 0,
      roleConfig: {
        page: 1,
        pagesize: 5
      },
      // 关闭弹窗  编辑
      dialogVisible: false,
      roleForm: {},
      rules: {
        name: [{ required: true, message: '角色名称不能为空', trigger: 'blur' }],
        description: [{ required: true, message: '角色描述不能为空', trigger: 'blur' }]
      },
      // 分配权限
      isShowPermission: false,
      // 权限选中的id数组
      checkList: [],
      // 所有权限数组
      permList: [],
      // 点击权限当前角色id
      roleId: ''
    }
  },
  computed: {
    ...mapState('user', ['userInfo'])
  },
  async created() {
    // 获取角色列表信息
    this.getRoleList()
    // 获取公司信息
    this.companyInfo = await companyInfoAPI(this.userInfo.companyId)
    // 获取权限列表
    const res = await getPermissionList()
    this.permList = listToTree(res, '0')
  },
  methods: {
    async showPermission(id) {
      const { permIds } = await getRoleDetailAPI(id)
      this.checkList = permIds
      this.isShowPermission = true
      this.roleId = id
    },
    async btnPermOK() {
      await assignPerm({
        id: this.roleId,
        permIds: this.checkList
      })
      this.$message.success('修改成功！')
      this.btnPermCancel()
    },
    btnPermCancel() {
      this.checkList = []
      this.isShowPermission = false
    },
    // 获取角色列表信息
    async getRoleList() {
      const { rows, total } = await getRoleListAPI(this.roleConfig)
      this.roleList = rows
      this.total = total
    },
    // 点击分页，发送请求，更新页面
    async currentChange(newPage) {
      this.roleConfig.page = newPage
      this.getRoleList()
    },
    // 关闭dialog框
    btnCancel() {
      this.dialogVisible = false
      this.roleForm = {}
      // 清理报错
      this.$refs.roleForm.resetFields()
    },

    // 点击编辑按钮   获取角色信息
    async editRole(id) {
      this.roleForm = await getRoleDetailAPI(id)
      this.dialogVisible = true
    },
    // 点击确认，提交表单，更新编辑、添加
    async onConfirm() {
      this.$refs.roleForm.validate(async(valid) => {
        if (valid) {
          if (this.roleForm.id) {
            // 编辑
            await updateRoleAPI(this.roleForm)
          } else {
            // 新增
            await addRoleAPI(this.roleForm)
          }
          this.getRoleList()
          this.$message.success('恭喜，操作成功！')
          this.dialogVisible = false
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    // 点击删除角色
    async deleteRole(id) {
      try {
        await this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          iconClass: 'el-icon-delete-solid'
        })
        this.$message({
          type: 'success',
          message: '恭喜，删除成功!'
        })
        await delRoleAPI(id)
        await this.getRoleList()
      } catch (error) {
        this.$message.info('已取消删除')
      }
    }
  }
}
</script>

  <style>

  </style>
